package edu.unc.comp410.colemangeer.data;

import static java.lang.System.out;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

import org.apache.log4j.Logger;

import edu.unc.comp410.colemangeer.model.Customer;

/**
 * Emulates DB table Customer with in-memory Java data structures. No persistent
 * storage, but this class acts as a Test Double for CustomerGateway.
 * 
 */
public class CustomerGatewayEmulator implements ICustomerGateway {

	// CONSTANTS
	private static int ROWLENGTH = 8;
	private static String INPUT_FILE_LOCATION = "resources/";
	private static String INPUT_FILE_NAME = "CustomerDB.in";

	private static Integer nextUnusedId = 1000;
	private static Logger log = Logger.getLogger(CustomerGatewayEmulator.class);
	
	// Data members
	Map<Integer, Customer> custTable = new HashMap<Integer, Customer>();

	public CustomerGatewayEmulator() 
	throws FileNotFoundException {
		log.warn(this.toString());
		out.println( "load from file");
		loadFromFile(INPUT_FILE_LOCATION + INPUT_FILE_NAME);
		out.println( "after loading, custTable.size() = " + custTable.size() );
		out.println( "customer 1 = " + custTable.get(1));
	}

	public void clear() {
		custTable.clear();
	}

	/**
	 * @return number of DB rows affected
	 */
	public int deleteById(int custId) {
		if (null == custTable.remove(custId))
			return 0;
		else
			return 1;
	}

	public Customer getById(Integer searchId) {
		return custTable.get(searchId);
	}

	public Set<Customer> getByName(String searchName) {
		final String regex = ".*" + searchName + ".*";
		Set<Customer> result = new HashSet<Customer>();
		Iterator<Integer> itr = custTable.keySet().iterator();
		while (itr.hasNext()) {
			Customer temp = custTable.get(itr.next());
			// if( temp.getName().equals( searchName ) )
			if (temp.getName().matches(regex))
				result.add(temp);
		}
		return result;
	}
	
	public Integer getNextUnusedId() {
		return nextUnusedId++;
	}

	public void insert(Customer theCustomer) {
		custTable.remove(theCustomer.getCustomerID());
		store(theCustomer);
	}
	
	public void loadFromFile( String inputFile )
	throws FileNotFoundException {
		Scanner inp = new Scanner( new File( inputFile ) );
		while( inp.hasNext() ) {
			String line = inp.nextLine();
			Customer c = Customer.string2Object(line);
			custTable.put( c.getId(), c );		
		}
	}

	public int size() {
		return custTable.size();
	}

	/**
	 * @return number of DB rows affected
	 */
	public void store(Customer theCustomer) {
		custTable.put(theCustomer.getCustomerID(), theCustomer);
	}

	// ?????????????????????
	public void store(String theValues) {
		// String to Customer
		// store(Customer);
	}

	//???????????????????????????????????????
	/**
	 * TESTING - backdoor
	 */
//	public void storeRow(String[] columns) {
//		assert (columns.length == ROWLENGTH);
//	}

	public boolean successfulDBconnection() {
		return true;
	}

	@Override
	public String toString() {
		return "*** CustomerGatewayEmulator";
	}

}
